package ClassData;

import ClassData.Visitor.IVisitor;
import ClassData.Visitor.IVisitorElement;

public class ConstantPoolUtf8_Info extends ConstantPoolInfo implements IVisitorElement {
	
	private byte [] myUtf8String; 

	public ConstantPoolUtf8_Info(byte[] myUtf8String) {
		this.myUtf8String=myUtf8String;	
	}

	public ConstantPoolUtf8_Info(String myString) {
		byte[] myUtf8String;
		int counter;
		
		myUtf8String= new byte[myString.length()];  // TODO Calculate Correct Size of UTF8 String
		
		for (counter=0;counter<myString.length();counter++) {
			myUtf8String[counter]=(byte)myString.charAt(counter);
		}
		
		this.myUtf8String=myUtf8String;	
	}

	public String getString() {
		String myString="";
		int stringSize;
		byte tmpByte;
		short byteCounter;
			
		byteCounter=0;
		stringSize=myUtf8String.length;
		
		while (byteCounter<stringSize) {
			tmpByte=myUtf8String[byteCounter];
			byteCounter++;
			if (tmpByte<0x80) {
				myString+=Character.toString((char)tmpByte);
			}
		}
			
		return(myString);	
	}

	public int getSize() {
		return(myUtf8String.length);
	}

	public byte[] getUtf8String() {
		return(myUtf8String);	
	}
	
	/**
	 * returns type of this constant pool entry
	 * 
	 */
	public int getType() {
		return(CONSTANT_Utf8);
	}

	@Override
	public void receive_Visitor(IVisitor visitor) {
		visitor.visit_ConstantPoolUtf8_Info(this);
	}
	
}
